<?php 

include 'config.php';
include 'db_connection.php';

$area = $_REQUEST['requests_array'];
$status = $_REQUEST['status'];

if(isset($_POST['Insert']) && $area != null){
  
  $filename = "./files/temp";
  $fd = fopen($filename, "w+");

  //fwrite($fd, utf8_encode($area) );
  fwrite($fd, $area );
  $file_contents = file_get_contents($filename);
//   echo $area;  

  fclose($fd);
  $fp = fopen( $filename, "r" );

  while ( !feof ( $fp) ) {

    $buffer = stream_get_line( $fp, 1024, "\n" );

    $requests = explode("\t", $buffer);

    $query = mysql_query("SELECT req_id, doc_name, est_cost FROM REQUESTS WHERE name = '$requests[1]'");
    $count = mysql_num_rows($query);
    $req_id = mysql_result($query, 0, 0);
    $doc_name = mysql_result($query, 0, 1);
    $est_cost = mysql_result($query, 0, 2);
//  echo $requests[1];
// echo $count;

// $count is always true
// because every Libuan might have
// more than one request
    if ($count == 0) {

      $date = strtotime($requests[9]);
      $time = date('d-m-y', $date);
// for($i = 0; $i<12; $i++){
//   print("<span>$status</span>");
// if($i==8){ print("<span>$time</span>");$i=9;}
//  print("<br>");
// }
// print("<span>$requests[4]</span>");
      $query = mysql_query("INSERT INTO REQUESTS (
	name,
	pass_num,
	med_unit,
	addm_num,
	pat_code,
	doc_name,
	request,
	est_cost,
	date_received,
	status,
	notes) 
	VALUES ( 
	'$requests[1]', 
	'$requests[2]',
	'$requests[3]',
	'$requests[4]',
	'$requests[5]',
	'$requests[6]',
	'$requests[7]',
	'$requests[8]',
	'$time',
	'$status',
	'$requests[11]')");

	if (!$query) {
	  die('Invalid INSERT query: ' . mysql_error());
	}

	$buffer = '';
	//echo $player_name;
   }//end of if

  else {
    $date = strtotime($requests[9]);
    $time = date('d-m-y', $date);
//      echo $est_cost;
//      echo $doc_name;
//      echo $req_id;
    $query = mysql_query("UPDATE REQUESTS SET
      date_received = '$time',
      
      est_cost = '$requests[8]'
      WHERE
      req_id = '$req_id';");
 //status = '$status',    
      if (!$query) {
	die('Invalid UPDATE query: ' . mysql_error());
      }
      $buffer = '';
    }//end of else

}//end of while

$fclose($fp);
header("location:admin.php");

}//end of if

else {
  echo "You didn't paste any text on the text area. However, if this message appears, well something isn't working as it should be..";
  header("location:admin.php");
}
$fclose($fp);

/*
      name = '$requests[1]', 
      pass_num = '$requests[2]', 
      med_unit = '$requests[3]',
      addm_num = '$requests[4]', 
      pat_code = '$requests[5]', 
      doc_name = '$requests[6]', 
      request = '$requests[7]', 
      est_cost = '$requests[8]',
      status = '$requests[10]'
*/
/*
include 'config.php';
include 'db_connection.php';

//$file = './uploads/player_data/players';
//$file = $_POST["players_array"];
$fp = fopen( $file, "r" );

while ( !feof ( $fp) ) {

    $buffer = stream_get_line( $fp, 1024, "\n" );
    $player = explode("\t", $buffer);

    $query = mysql_query("SELECT player_id FROM PLAYERS WHERE player_name = '$player[1]' AND outfit_number = '$player[0]' ");
    $count = mysql_num_rows($query);
    $player_id = mysql_result($query, 0);

    if ($count == 0) {

      $date = strtotime($player[3]);
      $time = date('y-m-d', $date);

      $query = mysql_query("INSERT INTO PLAYERS (
	outfit_number, 
	player_name, 
	position_name, 
	birth_date, 
	country, 
	total_games, 
	total_goals, 
	total_assists, 
	total_yellow,
	total_red,
	total_sent_offs,
	total_time) 
	VALUES ( 
	'$player[0]', 
	'$player[1]',
	'$player[2]',
	'$time',
	'$player[4]',
	'$player[5]',
	'$player[6]',
	'$player[7]',
	'$player[8]',
	'$player[9]',
	'$player[10]',
	'$player[11]')");

	if (!$query) {
	  die('Invalid INSERT query: ' . mysql_error());
	}

	$buffer = '';
    }//end of if

  else {

      $date = strtotime($player[3]);
      $time = date('y-m-d', $date);
      
      $query = mysql_query("UPDATE PLAYERS SET
	outfit_number = '$player[0]', 
	player_name = '$player[1]', 
	position_name = '$player[2]', 
	birth_date = '$time', 
	country = '$player[4]', 
	total_games = '$player[5]', 
	total_goals = '$player[6]', 
	total_assists = '$player[7]', 
	total_yellow = '$player[8]',
	total_red = '$player[9]',
	total_sent_offs = '$player[10]',
	total_time = '$player[11]' 
	WHERE
	player_id = '$player_id' ");

	if (!$query) {
	  die('Invalid UPDATE query: ' . mysql_error());
	}
      $buffer = '';

    }//end of else
}//end of while

$fclose($fp);
*/
?>
